﻿#include "TestHeader.h"
#include <time.h>

const long double pi = 3.1415926535897932384626433832795;
const long double to_radian = 0.01745329251994329576923690768489;

int func1(int num1 = 0, int num2 = 0, int num3 = 0)
{
	struct
	{ // 使用结构体类模仿闭包
		int func2(int in_num2 = 0, int in_num3 = 0) {
			struct
			{
				int func3(int in_in_num3 = 0) {
					return in_in_num3;
				}
			} f3obj;
			return in_num2 + f3obj.func3(in_num3);
		}
	} f2obj;
	return num1 + f2obj.func2(num2, num3);
}

int main() {
	FILE* fp;
	fp = fopen("test.txt", "r");
	char str[4096];
	fgets(str, 100, fp);
	printf("%s", str);

	// 创建 func1 的指针变量 pFunc
	int (*pFunc)(int num1, int num2, int num3) = func1;
	cout << "pFunc(" << 10 << ", " << 20 << ", " << 30 << ") = " << pFunc(10, 20, 30) << endl;

	cout << "sizeof(long double) = " << sizeof(long double) << endl;

	cout << "func1(" << 10 << ", " << 20 << ") = " << func1(10, 20) << endl;

	double a = sin(pi / 2.0f);
	cout << "sin(pi / 2) = " << a << endl;

	a = sin(30 * pi / 180); // 30 度，弧度制
	cout << "sin(30 * pi / 180.0) = " << a << endl << // 0.5
		"sin(30 * to_radian) = " << sin(30 * to_radian) << endl;

	a = sin(60 * pi / 180);
	cout << "sin(60 * pi / 180.0) = " << a << "    sqrt(3) / 2 = " << sqrt(3) / 2 << endl; // sqrt(3) / 2

	a = cos(60 * pi / 180);
	cout << "cos(60 * pi / 180.0) = " << a << endl; // 0.5

	a = tan(45 * pi / 180);
	cout << "tan(45 * pi / 180.0) = " << a << endl; // 1.0

	a = tan(60 * pi / 180);
	cout << "tan(60 * pi / 180.0) = " << a << endl; // sqrt(3)

	int ppa = 1;
	cout << "ppa = 1 and ppa++ + ppa++ + ppa++ + ppa++ + ppa++ + ppa++ = "
		<< ppa++ + ppa++ + ppa++ + ppa++ + ppa++ + ppa++ << endl
		<< "now ppa = " << ppa << endl; // 未定义行为

	// 测试循环
	char i = 10;
	while (i--) { // 输出: 9 8 7 6 5 4 3 2 1 0
		// 第一个输出为 9 因为 i-- 先执行，再输出
		printf("i = %d   ", i);
	}
	cout << endl;

	// 测试 TimeBase 循环
	//printf("TimeBase test:%s\n", TimeBase::format(TimeBase::now_time(), "%x年%x月%x日 %A %x:%x:%x:%x", true));
	printf("TimeBase test: now is %s\n", format_now_time("%x年%x月%x日 %A %x:%x:%x:%x", true));
	/*
	TimeIntervalBase::start_interval([](void) -> void {
		static int count = 0;
		cout << format_now_time("%x年%x月%x日 %A %x:%x:%x:%x", true) << " count = " << ++count << endl;
	}, 50000);
	TimeBase::sleep(1000000); // 1s
	TimeIntervalBase::pause_interval();
	cout << "count = " << TimeIntervalBase::get_interval_count() << endl;
	cout << "continue time is " << TimeIntervalBase::get_interval_continue_time() << endl;
	TimeBase::sleep(5000000); // 5s

	TimeIntervalBase::continue_interval();
	TimeBase::sleep(2000000); // 2s
	TimeBase::sleep(1000000); // 1s
	cout << "count = " << TimeIntervalBase::get_interval_count() << endl;
	cout << "continue time is " << TimeIntervalBase::get_interval_continue_time() << endl;
	cout << "stop!\n";
	TimeIntervalBase::stop_interval();
	cout << "count = " << TimeIntervalBase::get_interval_count() << endl;
	cout << "continue time is " << TimeIntervalBase::get_interval_continue_time() << endl;
	*/
	//TimeIntervalBase s1(1000);

	TestBase interval(100000);
	//TimeIntervalBase::start_interval([](void) -> void {
	//	static int count = 0;
	//	cout << format_now_time("%x年%x月%x日 %A %x:%x:%x:%x", true) << " count = " << ++count << endl;
	//}, 50000);
	int sum = 0;
	interval.run_func(
		[&sum](int num1, int num2, int num3) mutable -> void { // 即使返回 void 也会被调用
			sum = num1 + num2 + num3;
		}, 10, 20, 30);
	cout << "sum = " << sum << ", and sum reset to 0!\n";
	sum = 0;
	interval.start_load_func_interval(0,
		[&sum](int num1, int num2, int num3) mutable -> void { // 即使返回 void 也会被调用
		sum += num1 + num2 + num3;
		printf("sum = %d\n", sum);
		}, 1, 2, 3);
	//interval.start_interval_this();
	TimeBase::sleep(1000000); // 1s
	interval.pause_interval_this();
	cout << "interval count = " << interval.get_interval_count_this() << endl;
	cout << "continue time is " << interval.get_interval_continue_time_this() << endl;
	TimeBase::sleep(2000000); // 5s

	interval.continue_interval_this();
	TimeBase::sleep(2000000); // 2s
	cout << "interval count = " << interval.get_interval_count_this() << endl;
	cout << "continue time is " << interval.get_interval_continue_time_this() << endl;
	TimeBase::sleep(1000000); // 1s
	cout << "stop!\n";
	interval.stop_interval_this();
	cout << "interval count = " << interval.get_interval_count_this() << endl;
	cout << "continue time is " << interval.get_interval_continue_time_this() << endl;


#if defined _WIN32 || defined _WIN64
	system("pause");
#endif
	return 0;
}
